from tkinter import *
from setting import Setting
import threading
from YoloTask import *

# 初始化设置
setting = Setting()


def get_time(enter):
    jue_due_time = enter.get()
    if not '0' < jue_due_time < '9':
        lab['text'] = "请输入数字"
        return 0
    elif jue_due_time == '':
        return 0
    else:
        return (int(jue_due_time) * 60) * 1000


def start_task():
    setting.run_task = True

    if setting.task_thread is None:
        setting.exit_event = threading.Event()
        setting.task_thread = TaskThread(setting.exit_event)
        setting.task_thread.start()
    else:
        lab['text'] = "任务已经开始,请先结束任务!"

    # 获取输入框中的内容:
    setting.jue_dou_shi_chang = get_time(jue_dou_time_enter)  # 决斗场时间
    setting.fen_rao_shi_chang = get_time(fen_rao_time_enter)  # 丰饶之间时间

    task_list_time = [setting.jue_dou_shi_chang, setting.fen_rao_shi_chang]

    # 开启任务
    for t in range(0, len(task_list_time)):
        if task_list_time[t] > 0:
            if t == 0:  # 决斗场
                setting.jue_dou_flag = True

            elif t == 1:  # 丰饶之间
                setting.fen_rao_flag = True
            break


def stop():
    if setting.task_thread:
        setting.exit_event.set()
        setting.task_thread.stop()
        setting.task_thread = None


class TaskThread(threading.Thread):
    def __init__(self, exit_event):
        super().__init__()
        self.exit_event = exit_event
        self.stopped = False

    def run(self):
        while not self.stopped:
            if self.exit_event.is_set():
                break
            # 模拟任务执行
            task()

    def stop(self):
        global setting
        lab["text"] = "任务结束"
        # 初始化设置
        setting = Setting()
        self.stopped = True


def task():
    # 设置循环的时间长度，例如300秒（5分钟）

    # 开始决斗场任务
    if int(setting.jue_dou_shi_chang) > 0:

        if setting.dao_ji_shi <= 0:
            setting.dao_ji_shi = (setting.jue_dou_shi_chang / 1000 / 60)

            CountdownTimer("当前任务是决斗场")

        if setting.run_task and setting.jue_dou_flag:
            JueDouChang_task(setting.yolo)

    if int(setting.fen_rao_shi_chang) > 0:
        if setting.dao_ji_shi <= 0:
            setting.dao_ji_shi = (setting.fen_rao_shi_chang / 1000 / 60)
            CountdownTimer("当前任务是丰饶之间")

        if setting.run_task and setting.fen_rao_flag:
            fen_rao_task(setting.yolo)

    if not setting.run_task:
        lab["text"] = "任务结束!"
        stop()


class CountdownTimer:
    def __init__(self, task_str):

        self.task_str = task_str
        self.master = root
        self.label = lab
        self.start_countdown()

    def start_countdown(self):
        if setting.dao_ji_shi == 0:
            self.label.configure(text=f"{self.task_str}")
            setting.dao_ji_shi -= 1

            # 停止任务
            if setting.jue_dou_flag:
                setting.jue_dou_flag = False
                setting.fen_rao_flag = True  # 开启丰饶任务
                setting.jue_dou_shi_chang = -1

            elif setting.fen_rao_flag:
                setting.fen_rao_flag = False
                setting.fen_rao_shi_chang = -1
                # 停止所有任务
                self.label.configure(text=f"倒计时结束")
                setting.run_task = False

        elif setting.dao_ji_shi > 0:
            self.label.configure(text=f"{self.task_str},剩余还有{int(setting.dao_ji_shi)}分钟")
            setting.dao_ji_shi -= 1
            self.master.after(1000 * 60, self.start_countdown)  # 每隔1000ms（1分钟）调用一次自身


root = Tk()
# 设置窗口标题
root.title("火影忍者每日代肝")

# 设置窗口大小
root.geometry("400x300")  # 宽400像素，高300像素

# 顶部容器
top_frame = Frame(root, borderwidth=2, relief=GROOVE)
top_frame.pack(side=TOP, padx=10, pady=10)

lab = Label(top_frame, text="")
lab.pack(side=LEFT, padx=5, pady=5)
# 顶部容器

# 中间容器
left_frame = Frame(root)
left_frame.pack(side=LEFT, padx=10, pady=10)

# 决斗场容器
jue_dou_frame = Frame(left_frame, borderwidth=2, relief=GROOVE)
jue_dou_frame.pack(side=TOP, padx=2, pady=1)
jue_dou_chang_lab = Label(jue_dou_frame, text="决斗场")
jue_dou_chang_lab.pack(side=LEFT, padx=2, pady=5)

jue_dou_time_enter = Entry(jue_dou_frame)
jue_dou_time_enter.pack(side=LEFT, padx=2, pady=5)
jue_dou_time_enter['width'] = 3
# 决斗场容器

# 丰饶容器
fen_rao_frame = Frame(left_frame, borderwidth=2, relief=GROOVE)
fen_rao_frame.pack(side=TOP, padx=2, pady=1)

fen_rao_lab = Label(fen_rao_frame, text="丰饶之间")
fen_rao_lab.pack(side=LEFT, padx=2, pady=5)

fen_rao_time_enter = Entry(fen_rao_frame)
fen_rao_time_enter['width'] = 3
fen_rao_time_enter.pack(side=LEFT, padx=2, pady=5)
# 中间容器

# 底部容器
bottom_frame = Frame(root)
bottom_frame.pack(side=BOTTOM, padx=5, pady=5)
start_btn = Button(bottom_frame, text='开始任务', command=start_task)
start_btn.pack(side=TOP, padx=2, pady=2)

stop_btn = Button(bottom_frame, text='停止任务', command=stop)
stop_btn.pack(side=BOTTOM, padx=2, pady=2)
# 底部容器
root.mainloop()
stop()  # 关闭任务线程
